
	******************************************************************
	**
	**
	**		NAME:		GUY GROSSMAN, YOTAM MARGALIT, TAMAR MITTS
	**		DATE: 		December 15, 2021
	**		PROJECT: 	Israel Hayom
	**		DETAILS: 	This code generates table 2, and produce dataset model5_yeshuvlong_allparties.dta that would be used for producing Figure 8 
	**		Version: 	Stata MP 16
	**
	******************************************************************
	
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**
			**	**	**	**	**	**	**	**	**	**	**	**	**

clear
version 16
set scheme plotplain
set more off

capture cd "~/dropbox/Israeli Newspapers Study/JOP_REPLICATION/Dataverse submission"

* bring in dataset at locality level

use "Data/yeshuvlong_final.dta"

* additional cleaning
	
replace israelhayom_percent_e=0 if israelhayom_percent_e==. & year<2007
replace israelhayom_percent=0 if israelhayom_percent==. & year<2007
replace israelhayom_percent_cumulative=0 if israelhayom_percent_cumulativ==. & year<2007

drop if area_num >25

keep if BIG_rightbloc!=.
tabulate year, gen(YEAR)

* gen a running number for election years 	
egen yr = group(year)
move yr year

gen lpop= log(potential)
gen ltd = log(travel_dist)

* generate turnout var
gen turnout = voters/potential
replace turnout = 1 if turnout>1
replace turnout = turnout*100
tab year, summ(turnout)

* population share by age group
gen pop_share1829 = pop_M_18_29 / pop_total 
gen pop_share3049 = pop_M_30_49 /pop_total
gen pop_share5065 = pop_M_50_65 /pop_total
gen pop_share66 = pop_M_66 / pop_total
gen pop_adult = pop_M_18_29 + pop_M_30_49 + pop_M_50_65 + pop_M_66
 
 foreach var in pop_share1829 pop_share3049 pop_share5065 pop_share66{
	replace `var'=0 if `var'<0
 }

tabstat ltd lpop age_median_08 matriccert_pcnt_08 europe_pcnt_08 asia_pcnt_08 pop_share1829 pop_share3049 pop_share5065 pop_share66, by(yr)

corr ltd lpop age_median_08 potential matriccert_pcnt_08 europe_pcnt_08  asia_pcnt_08 pop_share1829 pop_share3049 pop_share5065 pop_share66 if yr==4

* generate otherpapers var for attentativeness measure
egen otherpapers=rowtotal(maariv_percent haaretz_percent globes_percent marker_percent calcalist_percent israelpost_percent makorrishon_percent) if year==2006
	bysort area_num: egen otherpapers2006=max(otherpapers)
	drop otherpapers
	
* rename parties	
ren Pisrael_beitenu Pib
ren BIG_rightbloc BRB
ren israelhayom_percent_electionyear IHE

* rename instruments
ren secular_max IVsm
ren secular_max07H1 IVsm07H1
ren sec_max06h2_07H1 IVsm06h2_07H1

tabstat IVsm IVsm07H1 IVsm06h2_07H1,  by(year)
corr IVsm IVsm07H1 IVsm06h2_07H1
drop IVsm

bysort area_num: egen IVsm=mean(IVsm07H1)
bysort area_num: egen IVsmp=mean(IVsm06h2_07H1)
tabstat IVsm IVsmp, by(year)
drop IVsm07H1 IVsm06h2_07H1

* generate variable of base level of support for right bloc and Likud
foreach var in BRB Plikud{
	bysort uniqueid: egen `var'0 = mean(`var') if yr<5
	bysort uniqueid: egen `var'Pre = mean(`var'0)
	drop `var'0
	}

* use LERI score to calculate mean ideology support at baseline 
table year uniqueid if uniqueid<6, c(mean leri_score )	
foreach var in leri_AVscore leri_score{
	bysort uniqueid: egen `var'0 = mean(`var') if yr<5
	bysort uniqueid: egen `var'Pre = mean(`var'0)
	drop `var'0
	}
		
xtset uniqueid yr
sort uniqueid yr	
estimates drop _all

* standardizing yediot 2007 correlates

foreach dv in religion_pcnt_08 asia_pcnt_08 europe_pcnt_08 matriccert_pcnt_08 lpop ltd pop_share1829 pop_share3049 pop_share5065 pop_share66 BRBPre leri_AVscorePre leri_scorePre {
	center `dv', standardize
	}

lab var c_religion_pcnt_08 "Share Jews"
lab var c_ltd "Distance to Tel Aviv (log)"
lab var c_lpop "Population (log)"
lab var c_matriccert_pcnt_08 "High-school matriculation"
lab var c_europe_pcnt_08 "Share European decent"
lab var c_asia_pcnt_08 "Share Asian decent"
lab var c_pop_share1829 "Pop share ages 18-29"
lab var c_pop_share3049 "Pop share ages 30-49"
lab var c_pop_share5065 "Pop share ages 50-65"
lab var c_pop_share66 "Pop share ages 66+"
lab var c_BRBPre "Right bloc vote share (baseline)"

* defining covariates
		
gl interactions i.yr#c.ltd i.yr#c.lpop i.yr#c.matriccert_pcnt_08 i.yr#c.europe_pcnt_08 i.yr#c.pop_share1829 i.yr#c.pop_share3049 i.yr#c.pop_share5065 i.yr#c.pop_share66 i.yr#c.religion_pcnt i.yr#c.asia_pcnt_08

xtset uniqueid yr

**************************************************
*  Regressions to produce the Panel A of Table 2 *
**************************************************

foreach var in BRB Plikud {
	
	* Two way fixed effects regression for BRB and Likud
	
	bootstrap, reps(250) seed(6375) cl(area) idcl(new_area) group(uniqueid) force : reghdfe `var' IHE [aw=potential], absorb(uniqueid yr) cl(area)
		estadd local covs "No"
		estadd local type "No"
		estadd local fe "Fixed"
		capture est sto `var'1
		
		
	bootstrap, reps(250) seed(6375) cl(area) idcl(new_area) group(uniqueid) force : reghdfe `var' IHE $interactions [aw=potential], absorb(uniqueid yr) cl(area)
		estadd local covs "Yes"
		estadd local type "No"
		estadd local fe "Fixed"
		capture est sto `var'2	

	* add flexible interaction with base level of 
	bootstrap, reps(250) seed(6375) cl(area) idcl(new_area) group(uniqueid) force : reghdfe `var' IHE $interactions c.`var'Pre##i.yr [aw=potential], absorb(uniqueid yr) cl(area)
		estadd local covs "Yes"
		estadd local type "Yes"
		estadd local fe "Fixed"
		capture est sto `var'3	
		
	* Single Fixed effects regressions for BRB and Likud
		
	bootstrap, reps(250) seed(6375) cl(area) idcl(new_area) group(uniqueid) force: areg `var' IHE yr [aw=potential], a(uniqueid) cl(area)
		estadd local covs "No"
		estadd local type "No"
		estadd local fe "Trend"
		capture est sto `var'4
		
	bootstrap, reps(250) seed(6375) cl(area) idcl(new_area) group(uniqueid) force : areg `var' IHE $interactions yr [aw=potential], absorb(uniqueid) cl(area)
		estadd local covs "Yes"
		estadd local type "No"
		estadd local fe "Trend"
		capture est sto `var'5	
		
		}	
		
	# delimit ;
		esttab BRB1 BRB2 BRB3 BRB4 BRB5 Plikud1 Plikud2 Plikud3 Plikud4 Plikud5
		using "Tables/Table_2.tex", replace
		keep(IHE _cons)
		varlabels(IHE "IH exposure" _cons "Constant")
		cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
		starlevels(* .10 ** .05 *** .01)
		mgroups("\textbf{Right Bloc}" "\textbf{Likud}", pattern(1 0 0 0 0 1 0 0 0 0)
		span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) nomtitles
		posthead("\midrule \multicolumn{10}{l}{\textbf{\textit{Panel A: Locality level}}} \\ \midrule")
		postfoot("")
		stats(covs type fe r2 N, labels("Covariates x year" "Base DV x year" "Year effects" "R2" "N")
		fmt(0 0 0 2 0)) collabels(none) label booktabs
		;
	#delimit cr	
	
	
		* Creating data for figure 8 using model 5
	
	preserve
	
	foreach var in BRB RIGHTBLOC Plikud Plabor Pib Pkadima Pbaityehudi Pshas {

		* add flexible interaction with base level of 
		
		bootstrap, reps(250) seed(6375) cl(area) idcl(new_area) group(uniqueid) force : areg `var' IHE $interactions yr [aw=potential], absorb(uniqueid) cl(area)
			estadd local covs "Yes"
			estadd local type "No"
			estadd local fe "Trend"
			capture est sto `var'5	
			capture est save "Data/model5_yeshuvlong_`var'", replace
			
			}		
		
		estimates use "Data/model5_yeshuvlong_BRB.ster"
		parmest, saving("Data/model5_yeshuvlong_allparties.dta", replace)
		
		
	foreach var in BRB RIGHTBLOC Plikud Plabor Pib Pkadima Pbaityehudi Pshas {
			
		estimates use "Data/model5_yeshuvlong_`var'.ster"
		parmest, saving("Data/model5_yeshuvlong_`var'.dta", replace)
		
		}
		
		use "Data/model5_yeshuvlong_allparties.dta", clear
		
		foreach var in RIGHTBLOC Plikud Plabor Pib Pkadima Pbaityehudi Pshas {
		
			append using "Data/model5_yeshuvlong_`var'.dta"
			capture rm "Data/model5_yeshuvlong_`var'.dta"
			capture erase "Data/model5_yeshuvlong_`var'.dta"
			capture rm "Data/model5_yeshuvlong_`var'.ster"
			capture erase "Data/model5_yeshuvlong_`var'.ster "
			
			}
			
		gen count = 1 if parm == "IHE"
		replace count = sum(count)
		
		local x = 0
		gen political_party = ""
		
	foreach var in BRB RIGHTBLOC Plikud Plabor Pib Pkadima Pbaityehudi Pshas {
		
		local x = `x' + 1 
		replace political_party = "`var'" if count == `x'
		
		}

	drop count
	keep if parm == "IHE"
	
	save "Data/model5_yeshuvlong_allparties.dta", replace
	
	restore

	* bring in dataset at media market level

	use "Data/arealong_final.dta", clear
		
	replace israelhayom_percent_electionyear =0 if israelhayom_percent_electionyear == . & year<2007

	replace israelhayom_percent=0 if israelhayom_percent==. & year<2007
	drop if area_num >25

	ren israelhayom_percent_electionyear IHE
	keep if IHE!=. 
	tabulate year, gen(YEAR)

	tabstat IHE, by(year)
	table area_num year, c(mean IHE)

	* gen running number for year 	
	egen yr = group(year)
	move yr year
		
	gen ltd = log(travel_dist)	
	gen lpop= log(potential)
	gen turnout = voters/potential
	su lpop turnout

	* generate turnout var
	replace turnout = 1 if turnout>1
	replace turnout = turnout*100
	tab year, summ(turnout)

	* generate otherpapers var for attentativeness measure
	egen otherpapers=rowtotal(maariv_percent haaretz_percent globes_percent marker_percent calcalist_percent israelpost_percent makorrishon_percent) if year==2006
		bysort area_num: egen otherpapers2006=max(otherpapers)
		drop otherpapers

	gl interactions i.yr#c.lpop i.yr#c.europe_pcnt_08 i.yr#c.matriccert_pcnt_08 i.yr#c.age_median_08 
	su $interactions

	gl covs lpop europe_pcnt_08 matriccert_pcnt_08 age_median_08 
	su $covs	

	ren BIG_rightbloc BRB

	ren secular_max IVsm
	ren secular_max07H1 IVsm07H1
	ren sec_max06h2_07H1 IVsm06h2_07H1

	tabstat IVsm IVsm07H1 IVsm06h2_07H1,  by(year)
	corr IVsm IVsm07H1 IVsm06h2_07H1
	drop IVsm

	bysort area_num: egen IVsm=mean(IVsm07H1)
	bysort area_num: egen IVsmp=mean(IVsm06h2_07H1)
	tabstat IVsm IVsmp, by(year)
	drop IVsm07H1 IVsm06h2_07H1
		
	* generate variable of base level of support for right bloc and Likud
	foreach var in BRB Plikud {
		gen `var'0 = `var' if yr==1
		bysort area_num: egen `var'Pre = mean(`var'0)
		drop `var'0
		}
		
	xtset area_num yr
	sort area_num yr	
	estimates drop _all	

	**************************************************
	*  Regressions to produce the Panel B of Table 2 *
	**************************************************

	set more off
	gl interactions i.yr#c.ltd i.yr#c.lpop i.yr#c.matriccert_pcnt_08 i.yr#c.europe_pcnt_08 i.yr#c.ltd i.yr#c.age_median_08
	su $interactions

	* Two way fixed effects regressions - BRB

	reghdfe BRB IHE [aw=potential], absorb(area_num year) cl(area_num)
		estadd local controls "No"
		estadd local type "No"
		estadd local fe "Fixed"
		capture est sto BRB1
		
	reghdfe BRB IHE $interactions [aw=potential], absorb(area_num year) cl(area_num)
		estadd local controls "Yes"
		estadd local type "No"
		estadd local fe "Fixed"
		capture est sto BRB2	
		
	reghdfe BRB IHE $interactions c.BRBPre##i.year  [aw=potential], absorb(area_num year) cl(area_num)
		estadd local controls "Yes"
		estadd local type "Yes"
		estadd local fe "Fixed"
		capture est sto BRB3
		
	* Single Fixed effects regressions - BRB

	areg BRB IHE year [aw=potential], a(area_num) cl(area_num)
		estadd local controls "No"
		estadd local type "No"
		estadd local fe "Trend"
		capture est sto BRB4	
		
	areg BRB IHE $interactions year [aw=potential], absorb(area_num) cl(area_num)
		estadd local controls "Yes"
		estadd local type "No"
		estadd local fe "Trend"
		capture est sto BRB5
		
	* Two way fixed effects regressions - Plikud
		
	reghdfe Plikud IHE [aw=potential], absorb(area_num year) cl(area_num)
		estadd local controls "No"
		estadd local type "No"
		estadd local fe "Fixed"
		capture est sto lik1
		
	reghdfe Plikud IHE $interactions [aw=potential], absorb(area_num year) cl(area_num)
		estadd local controls "Yes"
		estadd local type "No"
		estadd local fe "Fixed"
		capture est sto lik2
		
	reghdfe Plikud IHE $interactions c.PlikudPre##i.year [aw=potential], absorb(area_num year) cl(area_num)
		estadd local controls "Yes"
		estadd local type "Yes"
		estadd local fe "Fixed"
		capture est sto lik3
		
	* Single Fixed effects regressions - Plikud
		
	areg Plikud IHE year [aw=potential], a(area_num) cl(area_num)
		estadd local controls "No"
		estadd local type "No"
		estadd local fe "Trend"
		capture est sto lik4	
		
	areg Plikud IHE $interactions year [aw=potential], absorb(area_num) cl(area_num)
		estadd local controls "Yes"
		estadd local type "No"
		estadd local fe "Trend"
		capture est sto lik5
		
	# delimit ;
	esttab BRB1 BRB2 BRB3 BRB4 BRB5 lik1 lik2 lik3 lik4 lik5
			using "Tables/Table_2.tex", append
			keep(IHE _cons)
			varlabels(IHE "IH exposure" _cons "Constant")
			cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
			starlevels(* .10 ** .05 *** .01)
			prehead("")
			posthead("\midrule \multicolumn{10}{l}{\textbf{\textit{Panel B: Media market level}}} \\ \midrule")
			stats(controls type fe r2 N, labels("Covariates x year" "Base DV x year" "Year effects" "R2" "N")
			fmt(0 0 0 2 0)) collabels(, none) mlabels(, none) nonumbers label booktabs nonotes
			;
			#delimit cr	
			
			
